<?php
/**
 * 功能: PhpStorm.
 * User: 文丹
 * Date: 2018/5/7 0007
 * Time: 9:36
 */

namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Db;
class Index extends Controller{
    /**
     * @return mixed
     * 后台首页
     */
    public function index(){
        $account = session('name');
        $data = model("manager")->findAccount($account);
        $role_name = Db::name('role')->where('id',$data['role_id'])->value('name');
        $remind = Db::name('remind')->order('time desc')->find();
        $warning = Db::name('warning')->where('is_read',0)->select();
        $count = count($warning);
        /*echo "<pre>";
        print_r($data);
        die;*/
        $this->assign([
            'account'=>$data['account'],
            'warning'=>$warning,
            'count'=>$count,
            'role_name'=>$role_name,
            'remind'=>$remind,
        ]);
        //return view('');
        if(empty($account)){
            return $this->fetch('index:login');
        }else{
            return view('index/index');
        }
    }
    /**
     * pid预警
     */
    public function warning(){
        $res = Db::name('warning')
            ->where('is_read',0)
            ->paginate(10)
            ->each(function($item,$key){
                $user = Db::name('user')->where('phone',$item['phone'])->find();
                $item['nickname'] = $user['nickname'];
                $item['photo'] = $user['photo'];
                return $item;
            });

        $this->assign([
            'warning'=>$res
        ]);

        return view('');
    }

    /**
     * 阅读
     */
    public function read(){
        $res = Db::name('warning')
            ->where('is_read',0)
            ->update(['is_read'=>1]);
        if($res){
            return json(['msg'=>'阅读成功','ok'=>true]);
        }else{
            return json(['msg'=>'阅读失败','ok'=>false]);
        }
    }


public function welcome(){
    //商品
    $goods_type = Db::name('goods_type')->select();
    $arr = [];
    $data = [];
    foreach($goods_type as $k=>$v){
        $arr[] = $v['name'];
        $data[$v['name']] = Db::name('goods')->where('gt_id',$v['id'])->sum('browse');
    }
    //用户统计
    $users = Db::name('user')->count();
    $endDate = date('Y-m-d',time());
    $dayEnd = strtotime(date($endDate." 23:59:59"));
    $user_day = Db::name('user')
        ->whereTime('add_time','>=',time())
        ->whereTime('add_time','<=',$dayEnd)
        ->count();//用户今日数
    //今日活跃数
    $active = Db::name('browse')->whereTime('time','>=',$endDate)->count();

    $active_rate = ($active/$users)*100;

    $this->assign([
        'arr'=>$arr,
        'users'=>$users,
        'user_day'=>$user_day,
        'active'=>$active,
        'data'=>$data
    ]) ;
    return view('');
}


}